using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using InfoTech.VinhSon.Bussiness;
using InfoTech.Core.Repository;
using InfoTech.VinhSon.IRepository;

namespace InfoTech.VinhSon.Repository
{
    public partial class ArticleGroupRepository : BaseEntityRepository<VinhSonContext, ArticleGroup>, IArticleGroupRepository
    {

		#region Constructor 

        public ArticleGroupRepository(VinhSonContext context)
        {
            DataContext = context;
        }

        #endregion

        #region Implementation of IBaseRepository<VinhSonDataContext>

        public virtual void Delete(System.Int32 value)
        {
            var temp = SelectbyId(value);
            Delete(temp);
        }

        public virtual void Delete(System.Int32 value, out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Delete(IEnumerable<System.Int32> value)
        {
            var query = DbSet.Where(c => value.Contains(c.Id));
            Delete(query);
        }

        public virtual void Delete(IEnumerable<System.Int32> value, out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Delete(ArticleGroup value)
        {
            //if (DataContext.Entry(value).State == EntityState.Detached)
            //    DbSet.Attach(value);
            //DataContext.Entry(value).State = EntityState.Deleted;

			value.Inactive = true;
            value.LastModified = DateTime.Now;
            
            if (DataContext.Entry(value).State == EntityState.Detached)
                DbSet.Attach(value);
            DataContext.Entry(value).State = EntityState.Modified;
        }

        public virtual void Delete(ArticleGroup value, out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Delete(IEnumerable<ArticleGroup> value)
        {
            // SetEntityState(value, EntityState.Deleted);
            foreach (ArticleGroup item in value)
            {
                Delete(item);
            }
        }

        public virtual void Delete(IEnumerable<ArticleGroup> value, out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Insert(ArticleGroup value)
        {
			value.Inactive = false;
            value.LastModified = DateTime.Now;

            DbSet.Add(value);
        }

        public virtual void Insert(ArticleGroup value, out Exception exception)
        {
            try
            {
                Insert(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual IQueryable<ArticleGroup> Select()
        {
            return DbSet;
        }

        public virtual IQueryable<ArticleGroup> Select(out Exception exception)
        {
            try
            {
                IQueryable<ArticleGroup> query = Select();
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual IQueryable<ArticleGroup> Select(Expression<Func<ArticleGroup, bool>> predicate, string include = "")
        {
            IQueryable<ArticleGroup> query = DbSet;
            if (predicate != null)
                query = query.Where(predicate);
            if (!string.IsNullOrEmpty(include))
            {
                string[] s = include.Split(',');
                query = s.Aggregate(query, (current, s1) => current.Include(s1));
            }
            return query;
        }

        public virtual IQueryable<ArticleGroup> Select(Expression<Func<ArticleGroup, bool>> predicate, string include, out Exception exception)
        {
            try
            {
                IQueryable<ArticleGroup> query = Select(predicate, include);
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual IQueryable<ArticleGroup> Select<TKey>(Expression<Func<ArticleGroup, bool>> predicate = null, Expression<Func<ArticleGroup, TKey>> keySelector = null, ListSortDirection direction = ListSortDirection.Ascending, string include = "")
        {
            IQueryable<ArticleGroup> query = DbSet;
            if (predicate != null)
                query = query.Where(predicate);
            if (keySelector != null)
                query = direction == ListSortDirection.Descending ? query.OrderByDescending(keySelector) : query.OrderBy(keySelector);
            if (!string.IsNullOrEmpty(include))
            {
                string[] s = include.Split(',');
                query = s.Aggregate(query, (current, s1) => current.Include(s1));
            }
            return query;
        }

        public virtual IQueryable<ArticleGroup> Select<TKey>(Expression<Func<ArticleGroup, bool>> predicate, Expression<Func<ArticleGroup, TKey>> keySelector, ListSortDirection direction, string include, out Exception exception)
        {
            try
            {
                IQueryable<ArticleGroup> query = Select(predicate, keySelector, direction, include);
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual ArticleGroup SelectbyId(System.Int32 value)
        {
            ArticleGroup temp = DbSet.Find(value);
            return temp;
        }

        public virtual ArticleGroup SelectbyId(System.Int32 value, out Exception exception)
        {
            try
            {
                ArticleGroup temp = SelectbyId(value);
                exception = null;
                return temp;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual void Update(ArticleGroup value)
        {
			value.LastModified = DateTime.Now;

            if (DataContext.Entry(value).State == EntityState.Detached)
                DbSet.Attach(value);
            DataContext.Entry(value).State = EntityState.Modified;
        }

        public virtual void Update(ArticleGroup value, out Exception exception)
        {
            try
            {
                Update(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        #endregion

    }
}
